import axios, { type AxiosResponse } from "axios";


const instance = axios.create({
  baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000/api',
  timeout: 10000,
})
interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

instance.interceptors.response.use(
  (response: AxiosResponse) => {
    const { data } = response.data;
    return data
  },
  (error) => {
    return Promise.reject(error);
  }
);


/**
 * @param {string} url
 * @param {object} data
 * @param {object} params
 */
export function post<T>(url: string, params = {}): Promise<ApiResponse<T>> {
  return instance.post(url, {params});
}

/**
 * @param {string} url
 * @param {object} params
 */
export function get<T>(url: string, params = {}): Promise<ApiResponse<T>> {
  return instance.get(url, {params});

}
